package com.competition.prediction.controller;

import com.competition.prediction.common.Result;
import com.competition.prediction.dto.PlaceBetDTO;
import com.competition.prediction.entity.UserBet;
import com.competition.prediction.service.BettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/bet")
public class BettingController {

    @Autowired
    private BettingService bettingService;

    @PostMapping("/place/{userId}")
    public Result<String> placeBet(@PathVariable Long userId, @RequestBody @Validated PlaceBetDTO placeBetDTO) {
        try {
            boolean success = bettingService.placeBet(userId, placeBetDTO);
            if (success) {
                return Result.success("投注成功");
            }
            return Result.error("投注失败");
        } catch (Exception e) {
            log.error("投注失败", e);
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/history/{userId}")
    public Result<List<UserBet>> getUserBets(@PathVariable Long userId) {
        try {
            List<UserBet> bets = bettingService.getUserBets(userId);
            return Result.success(bets);
        } catch (Exception e) {
            log.error("获取用户投注记录失败", e);
            return Result.error("获取用户投注记录失败");
        }
    }
}